package io.terminus.lego.shinda.web.oauth2.client.converter

import org.springframework.security.authentication.RememberMeAuthenticationToken
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken
import org.springframework.security.core.Authentication
import org.springframework.security.oauth2.client.authentication.OAuth2AuthenticationToken
import org.springframework.security.oauth2.provider.token.DefaultUserAuthenticationConverter

/**
 *
 * JWTToken的用户认证信息转换器（转成map或从map提取）：最底层实现
 * @author wangmeng
 * @date 2018/4/14
 */
class MultityUserAuthenticationConverter : DefaultUserAuthenticationConverter() {

    override fun extractAuthentication(map: MutableMap<String, *>?): Authentication {
        return super.extractAuthentication(map)
    }

    /**
     * 存在以下几种认证
     * 1.UserNamePassword
     * 2.OAuth2
     * 3.RememberMe
     *
     */
    override fun convertUserAuthentication(userAuthentication: Authentication?): MutableMap<String, *> {
        if (userAuthentication is OAuth2AuthenticationToken) {
            println("------->       OAuth2Authentication Convert         <----------")
            val oAuth2User = userAuthentication.principal
        }
        if (userAuthentication is UsernamePasswordAuthenticationToken) {
            println("------->       UsernamePasswordAuthenticationToken Convert         <----------")

        }
        if (userAuthentication is RememberMeAuthenticationToken) {
            println("------->       RememberMeAuthenticationToken Convert         <----------")
        }
        return super.convertUserAuthentication(userAuthentication)
    }

}